# _*_coding:utf-8 _*_
# @Time    :2020/11/27 23:20
# @Author  :lyc
# @Email   :lyc0209@qq.com
# @FileName:send_email.py

import smtplib  # 发送邮件模块
from email.mime.text import MIMEText  # 定义邮件内容
from email.mime.multipart import MIMEMultipart  # 用于传送附件
import json


def send_email(msg):
    email = {}
    with open('user.json', encoding='utf-8') as f:
        email = json.load(f)["email"]

    # 发送邮箱服务器
    smtpserver = 'smtp.163.com'

    # 发送邮箱用户名密码
    user = email["user"]
    password = email["password"]

    # 发送和接收邮箱
    sender = email["user"]
    receives = email["receives"]

    # 发送邮件主题和内容
    subject = email["title"]
    content = msg


    # 构建发送与接收信息
    msgRoot = MIMEMultipart()
    msgRoot.attach(MIMEText(content, 'html', 'utf-8'))
    msgRoot['subject'] = subject
    msgRoot['From'] = sender
    msgRoot['To'] = ','.join(receives)

    # SSL协议端口号要使用465
    smtp = smtplib.SMTP_SSL(smtpserver, 465)

    # HELO 向服务器标识用户身份

    smtp.helo(smtpserver)
    # 服务器返回结果确认
    smtp.ehlo(smtpserver)
    # 登录邮箱服务器用户名和密码
    smtp.login(user, password)
    print("Start send email...")

    smtp.sendmail(sender, receives, msgRoot.as_string())

    smtp.quit()
    print("Send End！")
